Macromedia Pollster
Part III: How the Macromedia Flash Polling Application Works
We created the polling interface that end-users interact with using Macromedia Flash. It's simple and fairly self-explanatory. It presents a tabbed interface with two screens (see Figure 1):
- Question: This tab presents the poll/question as its possible answers. Users input their responses to the poll here.
- Results: This tab displays the aggregated results of the poll in a pie chart.

Figure 1. The Macromedia Flash interface of the polling interface, shown twice here. The Question tab on the left yields the poll's answers in the Results tab on the right.
The poll uses Macromedia Flash, Flash Remoting, and the Pie Chart component from Macromedia Flash Charting Components Set 2, available on DRK Volume 4.
Passing Poll Information to the Macromedia Flash Interface In order for the polling interface to function correctly, it needs access to three pieces of information:
- The ID of the poll
- The path to the Flash Remoting adaptor that the poll uses to connect to the server
- The server-side path to the ColdFusion component with which the poll communicates
Because we use the polling interface for multiple polls, we must insert the required information dynamically at runtime by passing the data into the movie using the FlashVars tags in the HTML that load the polling interface. This lets you to pass multiple name/values pairs to the Macromedia Flash application, which are then available to ActionScript within the application as soon as it runs.
Here is an example of the HTML required to load the polling application (watch for the line breaks added here to help readability):
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/
swflash.cab#version=6,0,0,0"
width="152" height="300" id="poll">
<param name="movie" value="/pollster/flash/poll.swf">
<param name="FlashVars" value="&id=2B042C87%2DAE1E%2D131F%2DCC933242C288113B&gateway=
%2Fflashservices%2Fgateway&servicepath=pollster%2Ecomponents
%2Ehandlers%2EFlashHandler">
<param name=quality value="high">
<embed src="/pollster/flash/poll.swf"
quality="high"
width="152" height="300" name="poll"
type="application/x-shockwave-flash"
FlashVars="&id=2B042C87%2DAE1E%2D131F%2DCC933242C288113B&gateway=
%2Fflashservices%2Fgateway&servicepath=pollster%2Ecomponents
%2Ehandlers%2EFlashHandler"
pluginspage="http://www.macromedia.com/go/getflashplayer">
</embed>
</object>
Notice that all the information is included twice: once within an Object tag and once within an Embed tag. This is necessary because there are two Macromedia Flash Player plug-in types: an ActiveX control used in Microsoft Internet Explorer (in Windows) and the Netscape Plug-in used on all other browsers (on other operating systems).
The following TechNotes give you more information about the tags and attributes that you can use with this poll-loading HTML code:
- Macromedia Flash Object and Embed Tag Syntax (TechNote 4150)
- Flash Object and Embed Tag Attributes (TechNote 12701)
The section that interests us is the FlashVars tag/attribute. For this article we will look at the Object tag syntax (but it also applies to the Embed tag).
<param name="FlashVars" value= "&id=2B042C87%2DAE1E%2D131F%2DCC933242C288113B&gateway= %2Fflashservices%2Fgateway&servicepath=pollster%2Ecomponents %2Ehandlers%2EFlashHandler">
The name/value pairs are passed to the polling interface in the form of a URL-encoded query string. This makes it possible to pass multiple name/value pairs.
The format of the string is as follows:
&name_1=value_1&name_2=value_2
Notice that each name/value pair is separated by the "&" character and that the values are URL-encoded.
The FlashVars string above makes the following ActionScript variables available on the main timeline of the polling application as soon as it runs:
id = "2B042C87-AE1E-131F-CC933242C288113B"; gateway = "/flashservices/gateway"; servicepath = "pollster.components.handlers.FlashHandler";
You can read more about the FlashVars tag/attribute in Using FlashVars to Pass Attributes to a SWF (TechNote 16417).
The polling interface uses the information passed in using the FlashVars tag/attribute to load the information and data about the specified poll:
PollingApplicationClass.prototype.init = function()
{
//..code snipped
this.setPollId(_root.id);
this.setServicePath(_root.servicepath);
this.setGatewayURL(_root.gateway);
this.loadPoll();
}
The loadPoll method uses this information to load the data from the server (using an instance of the Poll ActionScript class). Here is the loadPoll method (code not related to loading data is absent):
PollingApplicationClass.prototype.loadPoll = function()
{
this.poll = new Poll(this.pollId);
this.poll.setGatewayURL(this.gatewayURL);
this.poll.setServicePath(this.servicePath);
this.poll.addListener(this);
this.poll.loadPoll();
}
In most cases, you can use the default values for the gateway and servicePath variables.
Examining the Polling Interface Architecture
If you examine poll.fla, the Macromedia Flash source file for the polling interface, you might notice that it uses the same architecture as the Search Application sample application (also on DRK 4). That architecture is discussed in detail in the documentation for the Search Application, but we will go over it quickly here. (To read more about the architecture, refer to the Search Application documentation.)
The core of the movie is divided into these main classes/symbols:
- Controller Class: This is the main class of the application. It controls the views/screens and handles user interaction and poll data. It is contained within the classes/PollingApplicationClass.as ActionScript include file and associated with PollingApplicationSymbol within the Macromedia Flash symbol library.
- Screen/View Classes: These classes represent the views of the application. As we mentioned above, there are two views within the interface: the Question screen and Results screen. They are represented by the classes contained with the classes/QuestionScreenClass.as and classes/ResultsScreenClass.as ActionScript include files, and are both associated with their respective symbols within the Macromedia Flash symbol library (see Figure 2).

Figure 2. The Symbol Library within the poll.fla source file.
Using the Poll ActionScript Class
All client/server communication is encapsulated within a single ActionScript class called Poll. This is contained within the PollLibrary/Poll.as ActionScript include file. It is included within its own folder in order to make it easier to reuse.
Abstracting the client/server communication code within a custom class has a number of advantages:
- Provides a simple API for accessing the polling information
- Makes the code easier to use in other projects
- Lets the client/server communication methods (such as from Flash Remoting to XML) to change without affecting the rest of the application
Each poll interface represents a single instance of a Poll class. The PollingApplicationClass creates this instance and uses it to send and load data relating to the poll.
Below is a quick overview of the Poll class API:
Methods
- Poll(pollId): Constructor for the class. Takes an optional pollId, which identifies the poll that the instance represents.
- setGatewayURL(url): Specifies the URL to the Flash Remoting adaptor on the server.
- setServicePath(servicePath): Specifies the path to the component on the server that the Poll instance connects to in order to send and load data. In most cases, you can use the default value.
- setPollId(pollId): Specifies the pollId that identifies the poll that the instance represents. This can also be specified when calling the constructor.
- loadPoll(): Loads the information associated with the pollId specified in the Constructor or setPollId method. When the data loads, onQuestionLoad and onResultsLoad events will be broadcast. If an error occurs, the onError event will be broadcast.
- submitAnswer(optionId): This submits a user's answer to the server. It takes an optioned, which is associated with an answer and retrieved through the loadPoll method. When the data has been successfully submitted, it will broadcast an onQuestionSubmitLoad event. If an error occurs, it will broadcast an onError event.
Events
- onResultsLoad: This is broadcast when poll results have been loaded from the server. It passes along a RecordSet object, which contains aggregated poll results.
- onQuestionLoad: This is broadcast when the question associated with the pollId is loaded. It passes along an object with these properties:
- question: The question for the poll.
- options: An array of objects, each of which represents a single poll response. The objects contain the following two properties:
- optionId: The ID associated with the answer
- optionText: A string containing the question
- status: A string that specifies the status of the poll. Valid value are:
- active: The poll is active and can accept user submissions.
- inactive: The poll will not record user responses.
- invalid: The pollId passed in was invalid.
- hasVoted: A Boolean value that indicates whether the user has voted on the current poll.
- restrictVote: A Boolean value that indicates whether users are only allowed to vote once on the current poll.
- onQuestionSubmitLoad: This is broadcast when the user has successfully submitted a response to the poll. It passes along a RecordSet object, which contains the updated aggregated poll results.
- onError: This is broadcast if an error occurs. It passes along an info object that contains information about the error.
Inserting the Macromedia Flash Poll into a Web Page
Once you create a poll in the Pollster administration interface, you can insert it into a web page using the Polling Application Object Extension for Dreamweaver MX included on DRK Volume 4. This extension inserts the required HTML for displaying poll.swf.
To install the Polling Application Object Extension, you must have the Macromedia Extension Manager installed. If you have not installed the Macromedia Extension Manager, you may install it from the Macromedia website. Before you install any extension, make sure you close Dreamweaver MX.
Install the components by double-clicking PollingAppObj.mxp. This opens the Macromedia Extension Manager, which installs the components for you automatically. You can also install the components by first opening the Macromedia Extension Manager, selecting File > Install Extension, and navigating to PollingAppObj.mxp.
Once you have installed the extension, you can access it by selecting Insert > Interactive Images > Poll or by selecting the Media tab on the Insert menu and clicking the poll icon. You must save your web page before using the extension to insert the poll.
To set the Polling Application dialog box options, specify the following settings:
- Poll ID is the unique ID generated by the Pollster administration interface. It must be 35 characters long.
- Flash Remoting Gateway Path is the URL that poll.swf uses to connect to and load data from the Flash Remoting gateway. The path should be a URL like "/flashservice/gateway" or "http://www.domain.com/flashservices/gateway". The default value for the path is "/flashremoting/gateway/".
- Polling Component Path is the URL that poll.swf uses to connect to, retrieve data from, and submit data to the ColdFusion component using Flash Remoting. The default value for the path is pollster.components.handlers.FlashHandler.
After these items are set, click OK.
Dreamweaver inserts the HTML for poll.swf into the page and puts a copy of poll.swf in the same folder as the web page. You can upload your page and poll.swf to your server immediately or you can continue working on your page.